package com.tedu._05sspringmvc2.controller;

import com.tedu._05sspringmvc2.mapper.UserMapper;
import com.tedu._05sspringmvc2.pojo.dto.UserDTO;
import com.tedu._05sspringmvc2.pojo.dto.UserUpdateDTO;
import com.tedu._05sspringmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.tedu._05sspringmvc2.pojo.entity.User;

import java.util.List;

@RestController
public class RestFulUserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    public String addUser(UserDTO userDTO){
        System.out.println("开始执行注册操作......");
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        int num = userMapper.insert(user);
        return "注册成功";
    }
    @RequestMapping(value = "/users",method = RequestMethod.PUT)
    public String updateUser(UserUpdateDTO userUpdateDTO){
        System.out.println("开始修改操作");
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        int update = userMapper.update(user);
        return update>0?"修改成功":"修改失败";
    }
    @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Integer id){
        System.out.println("开始删除操作");
        System.out.println("id:"+id);
        int delete = userMapper.delete(id);
        return delete>0?"删除成功":"删除失败";
    }
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List<UserVO> findAll(){
        return userMapper.findAll();
    }
    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    public UserVO findById(@PathVariable Integer id){
        UserVO byId = userMapper.findById(id);
        return byId;
    }



































}
